package com.mscsoft.ui;

import com.mscsoft.tools.ConTools;
import com.mscsoft.tools.Msg;
import com.mscsoft.tools.MyHTTP;

import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

public class Register {

    @FXML
    private HBox cfmPwdBox;

    @FXML
    private Label cfmPwdLabel;

    @FXML
    private PasswordField cfmPwdText;

    @FXML
    private HBox emailBox;

    @FXML
    private Label emailLabel;

    @FXML
    private TextField emailText;

    @FXML
    private HBox pwdBox;

    @FXML
    private Label pwdLabel;

    @FXML
    private PasswordField pwdText;

    @FXML
    private Button regButton;

    @FXML
    private VBox regScene;

    @FXML
    private Button returnToLoginButton;

    @FXML
    private Button sentButton;

    @FXML
    private HBox verifCodeBox;

    @FXML
    private Label verifCodeLabel;

    @FXML
    private TextField verifCodeText;

    @FXML
    void sentVeriCode(ActionEvent event) {
        String spec = "http://" + MyHTTP.serverAddr + "/sendCheckCode";
        Msg<?> msg = MyHTTP.get(spec, new String[] { "mail=" + emailText.getText() });
        if (msg != null) {
            if (msg.getCode() == 200) {
                Alert alert = new Alert(Alert.AlertType.INFORMATION);
                alert.setTitle("信息");
                alert.setHeaderText("发送验证码成功！");
                alert.setContentText("请前往您的邮箱查看验证码！");
                alert.showAndWait();
            } else {
                Alert alert = new Alert(Alert.AlertType.WARNING);
                alert.setTitle("警告");
                alert.setHeaderText("发送验证码失败！");
                alert.setContentText(msg.getMsg());
                alert.showAndWait();
            }
        } else {
            Alert alert = new Alert(Alert.AlertType.ERROR);
            alert.setTitle("错误");
            alert.setHeaderText("连接服务器失败！");
            alert.setContentText("请稍后重试！");
            alert.showAndWait();
        }
    }

    @FXML
    void toRegister(ActionEvent event) {
        if (pwdText.getText().equals(cfmPwdText.getText())) {
            String[] args = new String[] { "account=" + emailText.getText(),
                    "password=" + ConTools.sha1(pwdText.getText()),
                    "checkCode=" + verifCodeText.getText() };
            Msg<?> msg = MyHTTP.get("http://" + MyHTTP.serverAddr + "/register", args);
            if (msg != null) {
                if (msg.getCode() == 200) {
                    Alert alert = new Alert(Alert.AlertType.INFORMATION);
                    alert.setTitle("信息");
                    alert.setHeaderText("注册成功！");
                    alert.setContentText("请返回上一级页面登录！");
                    alert.showAndWait();
                } else {
                    Alert alert = new Alert(Alert.AlertType.WARNING);
                    alert.setTitle("警告");
                    alert.setHeaderText("注册失败！");
                    alert.setContentText(msg.getMsg());
                    alert.showAndWait();
                }
            } else {
                Alert alert = new Alert(Alert.AlertType.ERROR);
                alert.setTitle("错误");
                alert.setHeaderText("连接服务器失败！");
                alert.setContentText("请稍后重试！");
                alert.showAndWait();
            }
        } else {
            Alert alert = new Alert(Alert.AlertType.WARNING);
            alert.setTitle("警告");
            alert.setHeaderText("两次输入的密码不一致！");
            alert.setContentText("请检查后重试！");
            alert.showAndWait();
        }
    }

}
